
<?php slot('libraryNav', 'active') ?>
<script type="text/javascript" src="/js/fileupload.js"></script>
<script>
	function reloadFilesList() {
		//fix to make sure the first call does not override the call to the tab content
		
			$('#public-library-upload-form').hide();
			$.get("<?php echo url_for('library/publicDetail')?>", {}, function(data){ $('#library_content').html(data);});
			
		}

	function uploadFormRef() {
		return '#public-library-upload-form';
	}
</script>


<?php 
use_helper('Library');
?>

<div id="wrapper" class="courses">
	<div id="content">
		<div id="main">
			<div class="main-heading">
				<form  id="search" action="" method="post">
					<fieldset>
						<input type="text"  id="library_title"  onfocus="this.value='';" value="Filter by term..." />

					</fieldset>
				</form>
				<h1>Library</h1>
			</div>
			<div class="profile-nav">
				<ul class="display-options">
				
				<?php if($upload_access == 'yes'):?>
					<li class="upload">
						<a class="btn_upload" href="#">Upload</a>
						<?php include_partial('library/publicForm');?>
					</li>
				<?php endif?>
					
					<li class="active"><a class="btn_detail" href="<?php echo url_for('library/publicDetail')?>">Detail view</a></li>
					<li><a class="btn_list" href="<?php echo url_for('library/publicList')?>">List view</a></li>
				</ul>
				<form class="sort-by" action="" method="post">
					<fieldset>
						<label>Sort By: </label>
						<select class="sort-select">
							<option value="new">Newest First</option>
							<option value="old">Oldest First</option>
							<option value="title">Title</option>
						</select>
					</fieldset>
				</form>
			</div>

			<span id="library_content">
			
			</span>
		</div>

		<div id="sidebar">
			<div class="module link-list with-tags">

				<h3 class="gradient dark">View by Type</h3>
				<div class="inner">
					<ul id="category_links">
						<li><a href="all">All Types</a></li>
						
						<?php foreach(LibraryPeer::getCategories() as $plural=>$singular):?>
							<li><a href="<?php echo $singular?>"><?php echo ucwords($plural)?></a></li>
						<?php endforeach?>
						
					</ul>
				</div>
			</div>

			<div class="module link-list">
				<h3 class="gradient light">Top Annotators</h3>
				<div class="inner">
				
				</div>
			</div>

			<div class="module link-list">
				<h3 class="gradient dark">Popular Authors</h3>

				<div class="inner">
				
				</div>
			</div>

			<div class="module books">
				<h3 class="gradient light">Popular Books</h3>
				<div class="inner">
					<ul>
						
					</ul>
				</div>
			</div>
		</div>
	</div>
</div>


<script>

//global variables to maintain state of searching/filtering choices
library_sortby = null;
library_filter = 'all';
library_view = null;
library_page = 1;
library_title = '';

	//returns the parameter string that should be used to filter the content 
	function getFilterParams(){

		return '?sortby='+library_sortby+'&filter='+library_filter+'&page='+library_page+'&title='+library_title;

	}

	//============page filters
	function bindPageLink()
	{
		$('.pager > ul').children().click(pageData);
		$('.prev').click(pageData);
		$('.next').click(pageData);
	}

	function pageData()
	{	$('.pager > ul').children().attr('class','');
		$(this).attr('class', 'current');
		
		library_page = parseInt($(this).children().eq(0).attr('href'));
		url = library_view + getFilterParams();
		//redisplay content
		$.get( url, {},
				function(data) {

			$('#library_content').html(data);
			bindPageLink();	
			bindFileDelete();					
				}
		);
		return false;		
	}
	

	
	
	//============================sort by filters
	
	//bind the sort by select box
	function bindSortSelect()
	{
		$(".sort-select").change(sortData);	
	}

	function sortData() {
        library_sortby = $(this).val()
		library_page = 1;
		url = library_view + getFilterParams();

		//redisplay content
		$.get( url, {},
				function(data) {

			$('#library_content').html(data);
			bindPageLink();	
			bindFileDelete();					
				}
		);
		return false;

    }
	

//============================title filters
	
	//bind the sort by select box
	function bindTitleFilter()
	{
		$("#library_title").change(filterTitle);

		$("#library_title").parent().parent().submit(function(){return false});	
	}

	function filterTitle() {
        library_title = $(this).val()
		library_page = 1;
		url = library_view + getFilterParams();

		//redisplay content
		$.get( url, {},
				function(data) {

			$('#library_content').html(data);
			bindPageLink();		
			bindFileDelete();				
				}
		);
		return false;

    }
	
	//================category filters
	
	//bind the category links
	function bindCategoryFilters()
	{
		//get filter links
		$('#category_links').children().click(displayCategory);
	}

	//display a category, the scope is within the element of the binding function
	function displayCategory()
	{
			library_filter = $(this).children().eq(0).attr('href');

			$('.main-heading > h1').html('Library &raquo; ' + library_filter.substr(0, 1).toUpperCase() + library_filter.substr(1));
			

			//set the current view to keep trac when filtering , and refresh the paging
			url = library_view;
			library_page = 1;
			//add parameters to the url 
			url = url + getFilterParams();
			
			//redisplay content
			$.get( url, {},
					function(data) {

				$('#library_content').html(data);
				bindPageLink();	
				bindFileDelete();						
					}
			);
			return false;
	}

	//===========================view filters	
	
	/**
	*  show tab content when clicked
	*/
	function displayView(element) {
		//redistribute css
		$('.display-options > li').attr('class', '');

		<?php if($upload_access=='yes'):?>
			$('.display-options > li').eq(0).attr('class', 'upload');
		<?php endif?>
		
		$(element).attr('class','active');


		//get the href for the clicked button
		url = $(element).children().eq(0).attr('href');
		//set the current view to keep trac when filtering
		library_view = url;
		//add parameters to the url 
		url = url + getFilterParams();
		
		//redisplay content
		$.get( url, {},
				function(data) {

				$('#library_content').html(data);
				bindPageLink();
				bindFileDelete();
				}
		);
		return false;
		
	}
	
	
	/**
	*  Bind view buttons
	*/
	function bindViews() {
		$('.display-options > li').click(function()
			{
				//if it is a drop down tab then show its form
				if($(this).hasClass('upload'))
				{
				}
				else
				{
					
					displayView(this); 
					return false;
				}

				
			});
	}


//upload form button==============
	
	//show dropdown for file upload
	function bindShowForm(){

		$('.btn_upload').click(
				function(){

					$('.upload-modal').show();

					//must make the link return false on click so that only that element is effected and not the form as well
					$('.btn_upload').children().eq(0).click(function(){return false});		
				});
	}




	//bind form cancle button
	function bindFormCancel()
	{
		$('#upload-cancel').click(
				function()
				{
					$('.upload-modal').hide();
				}
				);
	}

	//bind file delete link
	function bindFileDelete()
	{
		$('.library-file-delete').click(
				function()
				{
					
					//get the href for the clicked button
					url = $(this).attr('href');
					
					//redisplay content
					$.get( url, {},
							function(data) {
								$('.display-options > li').eq(<?php if($upload_access == 'yes'):?>1<?php else:?>0<?php endif?>).click();
							}
					);

					return false;
				}
				);
	}
	



	$(document).ready(function(){
		bindViews();

		<?php if($upload_access=='yes'):?>
		bindShowForm();
		bindFormCancel();
		<?php endif?>
		
		bindCategoryFilters();
		bindPageLink();
		bindSortSelect();
		bindTitleFilter();
		
		//show first view
		$('.display-options > li').eq(<?php if($upload_access == 'yes'):?>1<?php else:?>0<?php endif?>).click();
		
	});





	

</script>




